package com.zl.zlbaseproject.ui.home.fragment;


import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.zl.zlbaseproject.R;
import com.zl.zlbaseproject.base.fragment.BaseMvpFragment;
import com.zl.zlbaseproject.presenter.home.HomeFragmentComtract;
import com.zl.zlbaseproject.presenter.home.HomeFragmentPresenter;
import com.zl.zlbaseproject.widget.GlideImageLoader;
import com.zl.zlbaseproject.widget.ZlLayoutManager;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by 郑磊 on 2018/5/27.
 */

public class HomeFragment extends BaseMvpFragment<HomeFragmentComtract.Presenter> implements HomeFragmentComtract.View, OnRefreshListener {
    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.loading)
    ZlLayoutManager mLayoutManager;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;


    @Override
    protected HomeFragmentComtract.Presenter initPresenter() {
        return new HomeFragmentPresenter(this, getContext());
    }

    @Override
    protected void initBefore() {

    }

    @Override
    protected void initView() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mLayoutManager.showContent();
//        mRefreshLayout.getLayout().postDelayed(new Runnable() {
//            @Override
//            public void run() {
                mRefreshLayout.autoRefresh();

//            }
//        }, 3000);

    }


    private void initBanner() {
        ArrayList images = new ArrayList<>();
        images.add(R.mipmap.food_banner_home);
        images.add(R.mipmap.movie_banner_home);

        //设置banner样式
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);

//        mBanner.indicator_drawable_selected

        //设置图片加载器
        mBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        mBanner.setImages(images);
        //设置banner动画效果
        //        mBanner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
        mBanner.isAutoPlay(true);
        //设置轮播时间
        mBanner.setDelayTime(4000);
        //设置指示器位置（当banner模式中有指示器时）
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
//                CommonUtils.showToast(MainActivity.this, "点击了第几张图片" + position);
            }
        });
        mBanner.start();
    }


    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        mBanner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        mBanner.stopAutoPlay();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_home;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.LoadHomeData();
    }



    public void onLoadFailed() {
        mRefreshLayout.finishRefresh();
    }

    public void onLoadSuccess() {
        initBanner();
        mRefreshLayout.finishRefresh();
    }
}

